﻿using System;
using System.Collections.Generic;
using System.Linq;
using CoreGraphics;
using Foundation;
using mb;
using mb.iOS;
using Masonry;
using MyExtensions;
using UIKit;

namespace libmp
{

	public class MovieCollectionDelegate : UICollectionViewDelegateFlowLayout
	{
		public nfloat GetMinimumLineSpacingForSection(UICollectionView collectionView, UICollectionViewLayout layout, nint section)
		{
			return 2 * 5f;
		}
		public UIEdgeInsets GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout, nint section)
		{
			float dd = 10f;
			return new UIEdgeInsets(dd, dd, dd, dd);
			// return new UIEdgeInsets(0, 0, 0, 0);
		}

		public CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
		{
			var s = collectionView.Bounds.Width / 2 - 20;
			return new CGSize(s, s + 10);

		}
	}

	public class MovieCollectionSource : UICollectionViewSource
	{
		static ILog logger = mb.ServiceLocator.Instance.Get<ILog>();
		Random rnd = new Random();

		public UICollectionView CollectionView { get; set; }

		public List<int> Numbers { get; set; } = new List<int>();

		public List<nfloat> Heights { get; set; } = new List<nfloat>();

		public MovieCollectionSource(UICollectionView collectionView)
		{
			CollectionView = collectionView;

			for (int n = 0; n < 10; ++n)
			{
				Numbers.Add(n);
				Heights.Add(rnd.Next(0, 100) + 40f);
			}
		}

		public override nint NumberOfSections(UICollectionView collectionView)
		{
			return 2;
		}

		public override nint GetItemsCount(UICollectionView collectionView, nint section)
		{
			return Numbers.Count;
		}

		public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
		{
			// Get a reusable cell and set it's title from the item
			var cell = collectionView.DequeueReusableCell("Cell", indexPath) as MyCollectionViewCell;
			// cell.BackgroundColor = UIColor.Red;

			var imgView = cell.Subviews.ElementAtOrDefault(1) as UIImageView;
			var textView = cell.Subviews.ElementAtOrDefault(2) as UILabel;
			if (imgView == null)
			{
				imgView = new UIImageView();
				imgView.SetParent(cell);
				imgView.MakeConstraints(m =>
				{
					m.Left.EqualTo(cell);
					m.Right.EqualTo(cell);
					m.Top.EqualTo(cell);
					m.Bottom.EqualTo(cell).MultipliedBy(0.7f);
				});

				textView = new UILabel();
				textView.SetParent(cell);
				textView.MakeConstraints(m =>
				{
					m.Left.EqualTo(cell);
					m.Right.EqualTo(cell);
					m.Top.EqualTo(imgView.Bottom()).Offset(-10);
					m.Bottom.EqualTo(cell);
				});
				logger.Log("add textview", cell.GetNativeHash(), cell.Subviews.Length);

				cell.Layer.CornerRadius = 10;
				cell.Layer.BorderWidth = 1;
				cell.Layer.BorderColor = new CGColor(0.3f, 0.3f, 0.3f, 1f);
				imgView.Layer.CornerRadius = 10;
				imgView.Layer.MasksToBounds = true;
			}

			textView.Text = Numbers[(int) indexPath.Item].ToString();

			var i = (int) indexPath.Item;
			var img = UIImage.FromFile($"{App.HotStorageRoot}/res/adverise/ad{i%5+1}.jpg");
			imgView.Image = new UIImage(img.CGImage, 2, UIImageOrientation.Up);
			return cell;
		}
	}
}